﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YasharEl.Infrastructure.ResourcesManagement.Managers;
using System.Diagnostics.Contracts;
using YasharEl.Infrastructure.Diagnostics.Logging;
using YasharEl.Infrastructure.ResourcesManagement.Managers.Database;
using YasharEl.Infrastructure.ResourcesManagement.Configuration;

namespace YasharEl.Infrastructure.ResourcesManagement.Providers
{
    /// <summary>
    /// Data Resource Provider.
    /// </summary>
    /// <remarks>
    /// This provider loads an assembly and generates a <see cref="ExtendedComponentResourceManager" /> populated
    /// with resources embedded in the assembly or a satellite based on the resource base name within the assembly.
    /// <para>Note: a <see cref="ExtendedComponentResourceManager" /> can only manage one resource at a time even
    /// if an assembly has multiple resources. Additional Resource Managers will be required for each Resource set
    /// within an assembly.</para>
    /// </remarks>
    public class DataResourceProvider : ResourceProvider
    {
        #region Fields

        readonly DataProvider dataProvider;
        readonly string connectionString;

        #endregion

        #region Construction

        /// <summary>
        /// Initializes a new default instance of the <see cref="DataResourceProvider"/> class.
        /// </summary>
        public DataResourceProvider(DataProvider dataProvider, string connectionString, string resourceBaseName, ILogger<ResourceProvider> logger)
            : base(resourceBaseName, logger)
        {
            Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(connectionString), "connectionString could not be null or empty.");

            this.dataProvider = dataProvider;
            this.connectionString = connectionString;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Create a Resource Manager to manage the resource for an assembly resource provider
        /// </summary>
        /// <returns>
        /// A <see cref="ExtendedComponentResourceManager"/> instance
        /// </returns>
        /// <remarks>
        /// This method makes a type of <see cref="ExtendedComponentResourceManager"/> instance publicly available
        /// based on the database instance and base name retrieved during Initialisation
        /// </remarks>
        public override ExtendedComponentResourceManager CreateResourceManager()
        {
            // Check for first time use of resource manager
            if (ResourceManager == null)
            {
                // Generate a Resource Manager
                ResourceManager = new DataResourceManager(dataProvider, connectionString, ResourceBaseName);
                ResourceManager.IgnoreCase = true;
            }
            Logger.Info("Resource manager({0}) created...", dataProvider);

            return ResourceManager;
        }

        #endregion
    }
}
